clear all, close all, clc;
addpath(genpath(pwd));

IF_SERVER = 0;

if IF_SERVER
    root = '../_FGS/';
else
    root = '../../_FGS/';
end
    

score_root = 'recommendation_output/';

res_root = 'res_pool/';

msk_root = 'msk_fg/';

data_set_name = 'ADL';

feature_code = 'l';

if(length(data_set_name)>0)
    global_prefix = [data_set_name '_'];
else
    global_prefix = [];
end

data_base_save_name = [global_prefix 'data_base'];


if 0

    data_base{1}.name = 'GOPR2384_cab';
    data_base{2}.name = 'GOPR2382_cab';
    data_base{3}.name = 'GOPR2393_cab';

    data_base{1}.start = 175;
    data_base{2}.start = 150;
    data_base{3}.start = 20;

    data_base{1}.end = 11150;
    data_base{2}.end = 5300;
    data_base{3}.end = 3100;

    data_base{1}.rate = 25;
    data_base{2}.rate = 50;
    data_base{3}.rate = 20;
else
    data_base{1}.name = 'P_01';
    data_base{2}.name = 'P_02';
    data_base{3}.name = 'P_03';
    data_base{4}.name = 'P_04';
    data_base{5}.name = 'P_05';
    data_base{6}.name = 'P_06';
    

    data_base{1}.start = 250;
    data_base{2}.start = 2000;
    data_base{3}.start = 1000;
    data_base{4}.start = 1000;
    data_base{5}.start = 1000;
    data_base{6}.start = 1000;

    data_base{1}.end = 53750;
    data_base{2}.end = 85000;
    data_base{3}.end = 71000;
    data_base{4}.end = 47000;
    data_base{5}.end = 44000;
    data_base{6}.end = 46000;
    

    data_base{1}.rate = 250;
    data_base{2}.rate = 1000;
    data_base{3}.rate = 1000;
    data_base{4}.rate = 1000;
    data_base{5}.rate = 1000;
    data_base{6}.rate = 1000;
end


%%
% if ~IF_SERVER
%     data_base = data_base(1:2);
% end

score_root = [ '../' root score_root];
res_root = ['../' root res_root];
msk_root = ['../' root msk_root];

for i = 1:length(data_base)
    data_base{i}.frame_n = (data_base{i}.end - data_base{i}.start)...
                            /data_base{i}.rate+1;
end

classifier_n = 0;

for i = 1:length(data_base)
    try
        fname = [score_root global_prefix data_base{i}.name '_' feature_code ...
                    '_score_record.bin' ];
        data_base{i}.score_matrix = getMat( fname )';
    catch
        disp([fname ' unfound'])
        data_base{i}.score_matrix = ones(1,1);
    end
    
    try
        fname = [ score_root global_prefix data_base{i}.name '_' feature_code ...
                    '_corr_pos.bin' ];
        data_base{i}.corr_pos = getMat( fname )';
    catch
        disp([fname ' unfound'])
        data_base{i}.corr_pos = ones(1,1);
    end
    
    try
        fname = [ score_root global_prefix data_base{i}.name '_' feature_code ...
                    '_corr_neg.bin' ];
        data_base{i}.corr_neg = getMat( fname )';
    catch
        disp([fname ' unfound'])
        data_base{i}.corr_neg = ones(1,1);
    end
            
    
    if(sum(size( data_base{i}.score_matrix ) ) == 2 ...
        || ((i~=1) && (size(data_base{i}.score_matrix,2) ~= classifier_n * 5) ) )
    
        if i==1
            disp('score_matrix unfound on training data!')
        else
            disp('score_matrix unfound or unmatch on testing!')
            data_base{i}.score_matrix = rand(    data_base{i}.frame_n, ...
                                    classifier_n*5 );
        end
    end
    
    if(data_base{i}.frame_n < size( data_base{i}.score_matrix ,1 ) )
        data_base{i}.score_matrix = ...
            data_base{i}.score_matrix(1:data_base{i}.frame_n,:);
    end
    
    if i==1
        classifier_n = size(data_base{i}.score_matrix,2) / 5;        
        disp( [num2str( classifier_n ) ' classifiers in the pool'] )
    end
    
    if( size( data_base{i}.corr_pos ,1) ~= classifier_n )
        disp(['corr_pos in database ' num2str(i) ' unfound or unmatch']);
        data_base{i}.corr_pos = eye( classifier_n , classifier_n );
    end
    
    
    
    if( size( data_base{i}.corr_neg ,1) ~= classifier_n )
        disp(['corr_neg in database ' num2str(i) ' unfound or unmatch']);
        data_base{i}.corr_neg = eye( classifier_n , classifier_n );
    end        
        
end

%%
global_n = 10;

for i = 1:length(data_base)
    for j = 1: global_n
        fname = [score_root data_base{i}.name '_global_feat_' ...
                 num2str( j - 1 ) '.bin' ];
        temp = getMat( fname )';               
        
        temp = temp/255;
        
        data_base{i}.global_feat{j} = temp(1:data_base{i}.frame_n,:);
    end
end